////
////  Lextra_Junior_ueb_ITAppDelegate.m
////  Lextra Junior ueb IT
////
////  Created by Dirk Lieder on 26.05.11.
////  Copyright 2011 Phosphon. All rights reserved.
////
//
//#import "Lextra_Junior_ueb_ITAppDelegate.h"

#import "Lextra_Engine.h"
#import "ThemenViewController.h"
#import "WimmelListViewController.h"
#import "CardViewController.h"
#import "TabbarViewController.h"

#import "Lextra_Junior_ueb_ITAppDelegate.h"
#import "EnhancedSplitViewController.h"
#import "SpieleViewController.h"
#import "Global.h"

@implementation Lextra_Junior_ueb_ITAppDelegate
@class SpieleViewController;
@class WoerterViewController;
@synthesize window;
@synthesize viewController;
@synthesize themmenView;
@synthesize managedObjectContext=__managedObjectContext;
@synthesize managedObjectModel=__managedObjectModel;
@synthesize persistentStoreCoordinator=__persistentStoreCoordinator;

@synthesize tabBarController;
@synthesize splitviewWord_1;
@synthesize splitviewThemen_2;
@synthesize splitviewSpiele_3;
@synthesize spiel1Detail;
@synthesize impress;
@synthesize woerView;


-(void) makeTabbarSplitview
{
    NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
    //
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPad_tabbar1.png"]];
    [tabBarController.tabBar addSubview:imageView];
    [imageView release];
    int index = 0; 
    for (UIViewController *controller in tabBarController.viewControllers) 
    {
        if (index == 0) 
        {
            splitviewWord_1.tabBarItem = controller.tabBarItem;
            [controllers replaceObjectAtIndex:index withObject:splitviewWord_1];
            splitviewWord_1.delegate = woerView;//DELEGATE
        }
        if (index == 1) 
        {
            splitviewThemen_2.tabBarItem = controller.tabBarItem;
            [controllers replaceObjectAtIndex:index withObject:splitviewThemen_2];
            splitviewThemen_2.delegate = themmenView;
            
        }
        if(index == 2)
        {
            SpieleViewController *spieleMaster = [[SpieleViewController alloc] init];
            UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:spieleMaster] autorelease];
            splitviewSpiele_3.viewControllers = [NSArray arrayWithObjects:nav, spiel1Detail, nil];
            splitviewSpiele_3.tabBarItem = controller.tabBarItem;
            splitviewSpiele_3.delegate = spieleMaster;//DELEGATE
            spieleMaster.spieleSplitview = splitviewSpiele_3;
            spieleMaster.tabBarController = tabBarController;
            spieleMaster.woerView = woerView;
            [controllers replaceObjectAtIndex:index withObject:splitviewSpiele_3];
            [spieleMaster release];
        }
        index++;
    }
    tabBarController.delegate = self;
    tabBarController.viewControllers = controllers;
    
    currentViewController = @"Woerter";
}

- (BOOL)isPortrait 
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    return orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController1
{
    //ADDED BY LDHAI
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
        NSLog(@"VIEWCONTROLLER 1: %@",[viewController1 title]);
        if([viewController1 title] == nil)
        {
            //ADDED BY LDHAI
//            UILabel * labelWoerterView = [[UILabel alloc] initWithFrame:CGRectMake(900, 10, 100, 30)];
//            labelWoerterView.text = @"Wöster";
//            labelWoerterView.font = [UIFont boldSystemFontOfSize:15.0];
//            labelWoerterView.opaque = NO;
//            labelWoerterView.backgroundColor = [UIColor underPageBackgroundColor];
//            labelWoerterView.textAlignment = UITextAlignmentCenter;
//            labelWoerterView.textColor = [UIColor blackColor];
//            labelWoerterView.layer.cornerRadius = 10.0;
//            labelWoerterView.layer.borderWidth = 1;
//            labelWoerterView.layer.borderColor = [[UIColor blackColor] CGColor];
            
            //DETERMINE ORIENTATION
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPad_tabbar1.png"]];;
            if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
                [imageView setFrame:CGRectMake(0, 0, 768, 49)];
                [imageView setImage:[UIImage imageNamed:@"iPad_tabbar1-portrait.png"]];
                NSLog(@"PU");
            }
            else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
                [imageView setFrame:CGRectMake(0, 0, 768, 49)];
                [imageView setImage:[UIImage imageNamed:@"iPad_tabbar1-portrait.png"]];
                NSLog(@"PD");
            }
            else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
                [imageView setImage:[UIImage imageNamed:@"iPad_tabbar1.png"]];
                NSLog(@"LL");
            }
            else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
                [imageView setImage:[UIImage imageNamed:@"iPad_tabbar1.png"]];
                NSLog(@"LR");
            } 
            //DETERMINE ORIENTATION
            [self.tabBarController.tabBar addSubview:imageView];
            [imageView release];
            
            currentViewController = @"Woerter";
            if(woerterHintStatus == YES)
            {
//                [self.tabBarController.tabBar addSubview:labelWoerterView];
//                [labelWoerterView release];
            }
        }
        else
        {
            if([[viewController1 title] isEqualToString:@"Themen"])
            {
                UILabel * labelThemenView = [[UILabel alloc] initWithFrame:CGRectMake(900, 10, 100, 30)];
                labelThemenView.text = @"Themen";
                labelThemenView.font = [UIFont boldSystemFontOfSize:15.0];
                labelThemenView.opaque = NO;
                labelThemenView.backgroundColor = [UIColor underPageBackgroundColor];
                labelThemenView.textAlignment = UITextAlignmentCenter;
                labelThemenView.textColor = [UIColor blackColor];
                labelThemenView.layer.cornerRadius = 10.0;
                labelThemenView.layer.borderWidth = 1;
                labelThemenView.layer.borderColor = [[UIColor blackColor] CGColor];
                
                //DETERMINE ORIENTATION
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPad_tabbar2.png"]];;
                if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
                    [imageView setFrame:CGRectMake(0, 0, 768, 49)];
                    [imageView setImage:[UIImage imageNamed:@"iPad_tabbar2-portrait.png"]];
                    NSLog(@"PU");
                }
                else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
                    [imageView setFrame:CGRectMake(0, 0, 768, 49)];
                    [imageView setImage:[UIImage imageNamed:@"iPad_tabbar2-portrait.png"]];
                    NSLog(@"PD");
                }
                else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
                    [imageView setImage:[UIImage imageNamed:@"iPad_tabbar2.png"]];
                    NSLog(@"LL");
                }
                else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
                    [imageView setImage:[UIImage imageNamed:@"iPad_tabbar2.png"]];
                    NSLog(@"LR");
                } 
                //DETERMINE ORIENTATION
                [self.tabBarController.tabBar addSubview:imageView];
                [imageView release];
                
                currentViewController = @"Themen";
                if(themenHintStatus == YES)
                {
                    [self.tabBarController.tabBar addSubview:labelThemenView];
                    [labelThemenView release];
                }
                
            }
            else if([[viewController1 title] isEqualToString:@"Spiele"])
            {
                //DETERMINE ORIENTATION
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPad_tabbar3.png"]];;
                if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
                    [imageView setFrame:CGRectMake(0, 0, 768, 49)];
                    [imageView setImage:[UIImage imageNamed:@"iPad_tabbar3-portrait.png"]];
                    NSLog(@"PU");
                }
                else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
                    [imageView setFrame:CGRectMake(0, 0, 768, 49)];
                    [imageView setImage:[UIImage imageNamed:@"iPad_tabbar3-portrait.png"]];
                    NSLog(@"PD");
                }
                else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
                    [imageView setImage:[UIImage imageNamed:@"iPad_tabbar3.png"]];
                    NSLog(@"LL");
                }
                else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
                    [imageView setImage:[UIImage imageNamed:@"iPad_tabbar3.png"]];
                    NSLog(@"LR");
                } 
                //DETERMINE ORIENTATION
                [self.tabBarController.tabBar addSubview:imageView];
                [imageView release];
                
                currentViewController = @"Spiele";
            }
        }
        
        if ([viewController1 isKindOfClass:[ImpressumViewController class]])
        {
            //ADDED BY LDHAI
            //DETERMINE ORIENTATION
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPad_tabbar4.png"]];;
            if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
                [imageView setFrame:CGRectMake(0, 0, 768, 49)];
                [imageView setImage:[UIImage imageNamed:@"iPad_tabbar4-portrait.png"]];
                NSLog(@"PU");
            }
            else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
                [imageView setFrame:CGRectMake(0, 0, 768, 49)];
                [imageView setImage:[UIImage imageNamed:@"iPad_tabbar4-portrait.png"]];
                NSLog(@"PD");
            }
            else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
                [imageView setImage:[UIImage imageNamed:@"iPad_tabbar4.png"]];
                NSLog(@"LL");
            }
            else if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
                [imageView setImage:[UIImage imageNamed:@"iPad_tabbar4.png"]];
                NSLog(@"LR");
            } 
            //DETERMINE ORIENTATION
            //iPad_tabbar4-portrait.png - iPad_tabbar4.png
            [self.tabBarController.tabBar addSubview:imageView];
            [imageView release];
            
            currentViewController = @"Impress";
        }
    }
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application 
{
}
#pragma mark -
#pragma mark UITabBarControllerDelegate methods

#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}

- (void)dealloc {
    
    [viewController release];
    [window release];
    [__managedObjectContext release];
    [__managedObjectModel release];
    [__persistentStoreCoordinator release];
    [tabBarController release];
    [window release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // insert and show show Lextra Engine
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
//        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
        [self makeTabbarSplitview];
        self.window.rootViewController = self.tabBarController;
    }
    else{
        [self.window addSubview:viewController.view];
    }
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    [self saveContext];
}

- (void)awakeFromNib
{
}

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil)
    {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        } 
    }
}

#pragma mark - Core Data stack

/**
 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
 */
- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created from the application's model.
 */
- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil)
    {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Lextra_Junior_ueb_IT" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
    return __managedObjectModel;
}

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Lextra_Junior_ueb_IT.sqlite"];
    
    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    
    
    return __persistentStoreCoordinator;
}

#pragma mark - Application's Documents directory

/**
 Returns the URL to the application's Documents directory.
 */
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

@end
